package com.ttg.web.service.job.primitive.impl;

import com.google.common.collect.Lists;
import com.google.common.util.concurrent.ListenableFuture;
import com.ttg.common.annotation.TaskRunMethod;
import com.ttg.common.dto.PrimitiveBaseDTO;
import com.ttg.common.vo.TaskResultVO;
import com.ttg.model.dto.AcctPrimitiveDTO;
import com.ttg.model.pojo.BankAccountPrimitive;
import com.ttg.model.vo.AcctMctVipInfoVO;
import com.ttg.web.dao.BankAccountPrimitiveDao;
import com.ttg.web.juc.TasktemplateHelper;
import com.ttg.web.service.job.primitive.BankAccountPrimitiveService;
import com.ttg.web.service.job.JobCommon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Description：
 * Author: chenyou
 * Date: 2025/11/11 10:38
 * Company: ttg
 * Copyright: Copyright (c) 2025
 */
@Service
@TaskRunMethod(value = "saveAcctPrimitiveTask")
public class BankAccountPrimitiveServiceImpl extends JobCommon implements BankAccountPrimitiveService {
    @Value("${tlinx.isJuc:false}")
    private volatile Boolean isJuc;//是否启用异步回调
    @Autowired
    private TasktemplateHelper tasktemplateHelper;
    @Autowired
    private BankAccountPrimitiveDao bankAccountPrimitiveDao;

    @Override
    public TaskResultVO saveAcctPrimitive(PrimitiveBaseDTO reqDto) {
        Integer day = Integer.parseInt(reqDto.getDate());
        //每日定时从数仓获取商户原始数据

        //1.读取ftp文档
        //......
        //todo 模拟sftp拉取过程
        List<BankAccountPrimitive> primitives = JobCommon.testAccountPrimitive(day);

        //删除指定date旧数据
        bankAccountPrimitiveDao.deleteByDate(Integer.valueOf(day));
        //拆分,按批次执行
        List<ListenableFuture<TaskResultVO>> futures = new ArrayList<>();
        List<TaskResultVO> results = new ArrayList<>();
        List<List<BankAccountPrimitive>> partList = Lists.partition(primitives,500);
        for(List<BankAccountPrimitive> part:partList){
            AcctPrimitiveDTO dto = new AcctPrimitiveDTO();
            dto.setDay(day);
            dto.setPart(part);
            if(isJuc){
                futures.add(tasktemplateHelper.submit(this,dto));
            }else {
                TaskResultVO result = saveAcctPrimitiveTask(dto);
                results.add(result);
            }
        }
        return taskResult(results,futures,isJuc);
    }

    public TaskResultVO saveAcctPrimitiveTask(AcctPrimitiveDTO dto){
        Integer day = dto.getDay();
        List<BankAccountPrimitive> part = dto.getPart();
        List<Long> actNos = part.stream().map(o -> o.getActNo()).collect(Collectors.toList());
        //查询账户关联的会员和商户
        List<AcctMctVipInfoVO> acctMctVipInfoVOS = bankAccountPrimitiveDao.getMctVipInfoByActNos(day,actNos);
        for(BankAccountPrimitive primitive:part){
            Optional<AcctMctVipInfoVO> optional = acctMctVipInfoVOS.stream().filter(o -> o.getActNo().equals(primitive.getActNo())).findFirst();
            if(optional.isPresent()){
                AcctMctVipInfoVO acctMctVipInfoVO = optional.get();
                primitive.setVipId(acctMctVipInfoVO.getVipId());
                primitive.setMctNo(acctMctVipInfoVO.getMctNo());
            }
        }
        bankAccountPrimitiveDao.saveBatch(part);
        return taskReuslt(JobCommon.Status.SUCCESS.getValue(),false,"ok");
    }

}
